//
//  UIButton+Extension.swift
//  SinaWeibo
//
//  Created by 林柱强 on 16/2/3.
//  Copyright © 2016年 林柱强. All rights reserved.
//

import UIKit

extension UIButton {
    //在分类中 需要使用便利的构造方法  有可能实例化一个空对象
    //就是在指定的构造行数基础上  对其进行扩展
    //必须调用指定的构造函数  调用方式  self.
    //相对于 指定的构造方法
    //构造方法最大的一个特点 没有返回值
    
    //构造器
    convenience init(title: String?, backImage: String?, color: UIColor?, image: String = "", size: CGFloat = 14){
        
        //调用构造方法实例化
        self.init()
        if title != nil {
            setTitle(title!, forState: .Normal)
        }
        
        if backImage != nil {
            setBackgroundImage(UIImage(named: backImage!), forState: .Normal)
            setBackgroundImage(UIImage(named: backImage! + "_highlighted"), forState: .Highlighted)
        }
        
        titleLabel?.font = UIFont.systemFontOfSize(size)
        
        setImage(UIImage(named: image), forState: .Normal)
        
        if color != nil {
            setTitleColor(color, forState: .Normal)
        }
    }
    
    class func createButton(imageName: String, title: String) -> UIButton{
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(10)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        return btn
    }

}
